package com.motu.vertx.module.utility.webhandler;

import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import com.motu.vertx.module.utility.webtool.SearchCondition;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.sqlclient.Pool;

import java.util.ArrayList;
import java.util.List;

public class IncomemonitorHandler extends BaseWebHandler{
	public static final String tableName = "income_monitor";
	public static final String tableName_user = "income_monitor_user";
	public static final String tableName_mail = "income_monitor_mail";
	private static Pool jdbcClientProject = null;//监控配置 连接池
	private static Pool jdbcClientUser = null;//监控数据 连接池


	public static void init(Pool jdbcClient, Pool jdbcClientGame){
		jdbcClientProject = jdbcClient;
		jdbcClientUser = jdbcClientGame;
	}

	public static void add(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		addToTable(routingContext,jdbcClientProject, tableName,null);
	}

	public static void update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		updateToTable(routingContext, jdbcClientProject, tableName,null);
	}

	public static void getone(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getFromTable(routingContext, jdbcClientProject, tableName);

	}
	public static void delete(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		deleteFromTable(routingContext, jdbcClientProject, tableName);
	}

	public static void search(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		scList.add(new SearchCondition("itemid", "=", null, null));
		searchFromTable(routingContext, jdbcClientProject, json, tableName, scList);
	}

	public static void listMail(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		getTableList(routingContext, jdbcClientProject,tableName_mail);
	}

	public static void addMail(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		addToTable(routingContext,jdbcClientProject, tableName_mail,null);
	}

	public static void updateMail(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		updateToTable(routingContext, jdbcClientProject, tableName_mail,null);
	}

	public static void deleteMail(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		deleteFromTable(routingContext, jdbcClientProject, tableName_mail);
	}

	public static void list(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		if (json.containsKey("stime")) {
			json.put("stime", TimeUtils.getTimeStrIntValue(json.getString("stime")));
		}
		if (json.containsKey("etime")) {
			json.put("etime", TimeUtils.getTimeStrIntValue(json.getString("etime")));
		}
		scList.add(new SearchCondition("itemid", "=", null, null));
		scList.add(new SearchCondition("userid", "=", null, null));
		scList.add(new SearchCondition("stime", ">=", new String[]{"addtime"}, null));
		scList.add(new SearchCondition("etime", "<=", new String[]{"addtime"}, null));
		searchFromTable(routingContext, jdbcClientUser, json, tableName_user, scList);
	}

}
